package cn.iocoder.yudao.module.exam.service.questions;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.exam.controller.admin.questions.vo.*;
import cn.iocoder.yudao.module.exam.dal.dataobject.questions.QuestionsDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.exam.dal.mysql.questions.QuestionsMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.exam.enums.ExamErrorCodeConstants.QUESTIONS_NOT_EXISTS;

/**
 * 考试题库 Service 实现类
 *
 * @author Francis
 */
@Service
@Validated
public class QuestionsServiceImpl implements QuestionsService {

    @Resource
    private QuestionsMapper questionsMapper;

    @Override
    public Long createQuestions(QuestionsSaveReqVO createReqVO) {
        // 插入
        QuestionsDO questions = BeanUtils.toBean(createReqVO, QuestionsDO.class);
        questionsMapper.insert(questions);

        // 返回
        return questions.getId();
    }

    @Override
    public void updateQuestions(QuestionsSaveReqVO updateReqVO) {
        // 校验存在
        validateQuestionsExists(updateReqVO.getId());
        // 更新
        QuestionsDO updateObj = BeanUtils.toBean(updateReqVO, QuestionsDO.class);
        questionsMapper.updateById(updateObj);
    }

    @Override
    public void deleteQuestions(Long id) {
        // 校验存在
        validateQuestionsExists(id);
        // 删除
        questionsMapper.deleteById(id);
    }

    @Override
        public void deleteQuestionsListByIds(List<Long> ids) {
        // 删除
        questionsMapper.deleteByIds(ids);
        }


    private void validateQuestionsExists(Long id) {
        if (questionsMapper.selectById(id) == null) {
            throw exception(QUESTIONS_NOT_EXISTS);
        }
    }

    @Override
    public QuestionsDO getQuestions(Long id) {
        return questionsMapper.selectById(id);
    }

    @Override
    public PageResult<QuestionsDO> getQuestionsPage(QuestionsPageReqVO pageReqVO) {
        return questionsMapper.selectPage(pageReqVO);
    }

}